package ltd.hxya.novel.analysis.consume;

import ltd.hxya.novel.analysis.event.CrawlFinisherEvent;
import ltd.hxya.novel.analysis.event.GroupFinisherEvent;
import ltd.hxya.novel.analysis.mapper.HiveDataPartMapper;
import ltd.hxya.novel.analysis.mapper.HiveRowDataMapper;
import ltd.hxya.novel.analysis.service.impl.HiveRowDataServiceImpl;
import ltd.hxya.novel.common.config.ThreadPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.sql.SQLException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Component
public class AnalysisConsume {

    @Autowired
    private HiveRowDataServiceImpl hiveRowDataService;

    @Autowired
    private ApplicationEventPublisher publisher;

    @Autowired
    private HiveRowDataMapper hiveRowDataMapper;

    @Autowired
    private HiveDataPartMapper hiveDataPartMapper;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Value("${category}")
    private String category;

    @Async
    @EventListener
    public void crawlFinish(CrawlFinisherEvent event){
        //消费消息
        String[] split = category.split(",");
        for (String cate : split) {
            hiveDataPartMapper.deleteByPartition(cate);
        }
        hiveRowDataService.dataCategory();
        //如果分组成功，则删除当前数据
//        hiveRowDataMapper.delete(null);
        //删除分区中的数据

        publisher.publishEvent(new GroupFinisherEvent(this));
        /*CompletableFuture.runAsync(()->{

        });*/
    }

    @Async
    @EventListener
    public void groupFinish(GroupFinisherEvent event) throws SQLException, IOException, ClassNotFoundException, InterruptedException {
        hiveRowDataService.analysisData();

    }
}
